// pages/community/community.js
const app = getApp();
const request = require('../../utils/request.js');
const config = require('../../utils/config');
const util = require('../../utils/util');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    colorTheme: app.globalData.colorTheme,
    TabCur: 0,
    scrollLeft:0,
    pageNum: 1,
    pageSize: 10,
    params: {},
    postList: [],
    forumList: [],
    isResult: true
  },

  /**
   * 切换导航标签
   */
  tabSelect(e) {
    let params = {};
    let index = e.currentTarget.dataset.id;
    if(index !== 0){
      params.forumId = this.data.forumList[index].id;
    }
    this.setData({
      TabCur: index,
      scrollLeft: (index-1)*60,
      params: params,
      postList: [],
      pageNum: 1
    });
    this.getPostList(1, 10, this.data.params);
  },

  /**
   * 获取帖子列表
   */
  getPostList(pageNum, pageSize, params){
    let token = wx.getStorageSync('token');
    let url = config.SERVER_URL + 'index/list/post/?pageNum=' + pageNum + '&pageSize=' + pageSize;
    params = params ? params : {};
    let that = this;
    wx.showLoading({
      title: '加载ing',
    })
    request.requestGetApi(url, token, params, 'application/json').then(res=>{
      if(res.status === config.STATUS_OK){
        let isResult = res.data.rows.length >= pageSize;
        res.data.rows.forEach(item => {
          item.images = util.filterImages(item.content);
          item.content = util.filterContent(item.content);
        });
        this.setData({
          isResult,
          postList: that.data.postList.concat(res.data.rows)
        });
      }
    }).catch(err=>{
      app.failFun(err);
    }).finally((res) => {
      app.completeFun(res);
    });
  },

  /**
   * 获取模块列表
   */
  getForumList(){
    let that = this;
    let url = config.SERVER_URL + 'forum/list/all';
    request.requestGetApi(url, {}, {}, 'application/json').then(res=>{
      if(res.status === config.STATUS_OK){
        res.data.unshift({
          id: 0,
          name: '全部'
        })
        that.setData({
          forumList: res.data
        });
      }
    }).catch(err=>{
      app.failFun(err);
    });
  },

  /**
   * 点赞帖子或取消点赞帖子
   */
  likeOrunLikePost(e){
    if(!app.checkedUserLoading()){
      return;
    }
    let postId = e.detail.postId;
    let that = this;
    let token = wx.getStorageSync('token');
    let url = config.SERVER_URL;
    let postList = that.data.postList;
    postList.find((item, index)=>{
      if(item.id === postId){
        if(item.like){
          url += 'post/unLike/' + postId;
          item.likeCount--;
        }else{
          url += 'post/like/' + postId;
          item.likeCount++;
        };    
        item.like = !item.like;
      };
    });
    request.requestPostApi(url, token, {}, 'application/json').then(res=>{
      if(res.status === config.STATUS_OK){
        that.setData({
          postList
        });
      }
    }).catch(err=>{
      app.failFun(err);
    })
  },

  /**
   * 预览图片
   */
  viewImage(e){
    let imageUrls = this.data.postList[e.detail.postIndex].images;
    wx.previewImage({
      current: imageUrls[e.detail.imgIndex], // 当前显示图片的http链接
      urls: imageUrls // 需要预览的图片http链接列表
    })
  },
  

  /**
   * 跳转到帖子详情页
   */
  toPostInfo(e){
    let url = '/pages/post/post?postId=' + e.detail.postId;
    if(e.detail.jump){
      url += '&jump=' + e.detail.jump;
    }
    wx.navigateTo({
      url
    });
  },

  /**
   * 跳转到编写帖子页面
   */
  toPublishPost(){
    if(!app.checkedUserLoading()){
      return;
    }
    wx.navigateTo({
      url: '/pages/editor/editor'
    });
  },

  /**
   * 跳转到搜索页面
   */
  toSearch(){
    wx.navigateTo({
      url: '/pages/search/search'
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    this.getPostList(this.data.pageNum, this.data.pageSize, this.data.params);
    this.getForumList();
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      postList: [],
      pageNum: 1
    });
    this.getPostList(this.data.pageNum, this.data.pageSize, this.data.params);

    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if(!this.data.isResult){
      return;
    }
    let pageNum = this.data.pageNum + 1;
    this.setData({
      pageNum: pageNum
    });
    this.getPostList(pageNum, this.data.pageSize, this.data.params);
  }
})